package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.RefundMapper;
import com.gxa.pingan.pojo.Refund;
import com.gxa.pingan.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author lijianmin
 * @date 2021/10/25  15:35:00
 */
@Service("refundService")
@Transactional(rollbackFor = {Throwable.class}, propagation = Propagation.REQUIRED)
public class RefundServiceImpl implements RefundService {

    @Autowired
    private RefundMapper refundMapper;

    /**
     * 退款列表查询的方法,orderType如果为空,则进行全查询,不为空则进行订单状态查询
     *
     * @param orderType 订单状态
     * @param page      页码
     * @param size      每页记录数
     * @return 返回查询到的集合以及总数
     */
    @Override
    public List<Refund> getList(Integer orderType, Integer page, Integer size) throws BusinessException {
        if (orderType != null && orderType != 0 && orderType != 1 && orderType != 2) {
            throw new BusinessException(ExceptionCode.NO_TYPE);
        }
        if (page != null) {
            if (page <= 0) {
                throw new BusinessException(ExceptionCode.PAGE_ERROR);
            } else {
                page = (page - 1) * size;
            }
        } else {
            page = 0;
        }

        if (size != null) {
            if (size <= 0) {
                throw new BusinessException(ExceptionCode.SIZE_ERROR);
            }
        } else {
            size = 10;
        }

        //调用getList方法
        return refundMapper.getList(orderType, page, size);
    }

    /**
     * 退款列表查询总数的方法,orderType如果为空,则进行全查询计数,不为空则进行订单状态查询计数
     *
     * @param orderType 订单状态
     * @return 返回查询到的集合以及总数
     */
    @Override
    public Integer getListCount(Integer orderType) throws BusinessException {
        //调用getList方法,分页传递空,然后调用size方法计数  会根据orderType是否为空分别进行查询
        return refundMapper.getList(orderType, null, null).size();
    }

    /**
     * 驳回退款的方法(修改订单状态为已拒绝(1))
     *
     * @param orderNum  修改状态订单的orderNum
     * @param rejReason 驳回退款的理由
     * @return 返回修改后的数据和是否返回成功
     */
    @Override
    public Boolean refuseRefund(String orderNum, String rejReason) throws BusinessException {
        Refund refund = refundMapper.selectByOrderNum(orderNum);
        //如果此订单编号未查询到 则抛出该订单不存在异常
        if (refund == null) {
            throw new BusinessException(ExceptionCode.NO_DATA_ORDER);
        }
        //如果此订单编号的订单状态已经为拒绝退款 则抛出重复修改订单状态异常
        if (refund.getOrderType().equals(1)) {
            throw new BusinessException(ExceptionCode.REPEAT_UPDATE_STATUS);
        }
        Integer integer = refundMapper.refuseRefund(orderNum, rejReason, new Timestamp(System.currentTimeMillis()));
        return integer != 0;
    }

    /**
     * 退款的方法(修改订单状态为退款完成(2))
     *
     * @param orderNum 修改状态订单的orderNum
     * @return 返回修改后的数据和是否返回成功
     */
    @Override
    public Boolean refund(String orderNum) throws BusinessException {
        Refund refund = refundMapper.selectByOrderNum(orderNum);
        //如果此订单编号未查询到 则抛出该订单不存在异常
        if (refund == null) {
            throw new BusinessException(ExceptionCode.NO_DATA_ORDER);
        }
        //如果此订单编号的订单状态已经为同意退款 则抛出重复修改订单状态异常
        if (refund.getOrderType().equals(2)) {
            throw new BusinessException(ExceptionCode.REPEAT_UPDATE_STATUS);
        }
        Integer integer = refundMapper.refund(orderNum, new Timestamp(System.currentTimeMillis()));

        return integer != 0;
    }
}
